//package com.xiguaji.tiktok.task.blogger.event;
//
//import com.google.common.eventbus.AllowConcurrentEvents;
//import com.google.common.eventbus.Subscribe;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerLogRepository;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerMainRepository;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerTypeRepository;
//import com.gugee.tiktok.common.model.dto.SlimBloggerDto;
//import com.gugee.tiktok.common.model.mysql.BloggerMain;
//import com.gugee.tiktok.common.model.mysql.BloggerType;
//import com.xiguaji.boot.eventbus.Event;
//import com.xiguaji.boot.eventbus.EventBusTemplate;
//import com.xiguaji.boot.eventbus.IEventListener;
//import com.xiguaji.tiktok.task.core.ScheduleTemplate;
//import com.xiguaji.tiktok.task.ranking.client.BloggerClient;
//import com.xxl.job.core.log.XxlJobLogger;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Optional;
//import java.util.concurrent.ForkJoinPool;
//
//@Component
//@Slf4j
//public class RefreshTopBloggerMainEventListener implements IEventListener<RefreshTopBloggerMainEventListener.RefreshTopBloggerMainEvent> {
//    @Autowired
//    BloggerLogRepository bloggerLogRepository;
//
//    @Autowired
//    BloggerMainRepository bloggerMainRepository;
//
//    @Resource(name ="BloggerClientRanking")
//    BloggerClient bloggerClient;
//
//    @Autowired
//    @Lazy
//    EventBusTemplate eventBusTemplate;
//
//    List<BloggerType> bloggerTypes = new ArrayList<>();
//
//    @Autowired
//    BloggerTypeRepository bloggerTypeRepository;
//
//
////    String topBloggerMain = "RefreshTopBloggerMain";
//
////    ForkJoinPool forkJoinPool = ThreadPoolUtils.forkJoinPool(10, topBloggerMain);
//
//    @Autowired
//    ScheduleTemplate scheduleTemplate;
//
//    @PostConstruct
//    public void initConstruct(){
//        BloggerType bloggerType = new BloggerType();
//        bloggerType.setId(0);
//        bloggerType.setName("ALL");
//        this.bloggerTypes.add(bloggerType);
//        this.bloggerTypes.addAll(bloggerTypeRepository.getBloggerTypeByIsDisplay(true));
////        scheduleTemplate.attach(topBloggerMain, forkJoinPool);
//    }
//
////    @Override
////    @Subscribe
////    @AllowConcurrentEvents
////    public void handle(RefreshTopBloggerMainEvent event) {
////        XxlJobLogger.log("RefreshTopBloggerMainEvent START typeSize:{}", bloggerTypes.size());
////        long startTime = System.currentTimeMillis();
////        try{
////            forkJoinPool.submit(() ->
////                    bloggerTypes.parallelStream().forEach( b-> run(b,event))
////            ).get();
////
////
////        } catch (Exception e){
////            XxlJobLogger.log(e);
////        }
////        XxlJobLogger.log("RefreshTopBloggerMainEvent END typeSize:{} use time:{}s",bloggerTypes.size(),(System.currentTimeMillis()-startTime)/1000);
////
////    }
//
//    @Override
//    @Subscribe
//    @AllowConcurrentEvents
//    public void handle(RefreshTopBloggerMainEvent event) {
//       XxlJobLogger.log("RefreshTopBloggerMainEvent START typeSize:{}", bloggerTypes.size());
//       long startTime = System.currentTimeMillis();
//       Thread single= new Thread(new SingleTask(this,event));
//        single.start();
//        try {
//            single.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            XxlJobLogger.log(e);
//        }
//        XxlJobLogger.log("RefreshTopBloggerMainEvent END typeSize:{} use time:{}s",bloggerTypes.size(),(System.currentTimeMillis()-startTime)/1000);
//
//    }
//    static class SingleTask implements Runnable{
//        private RefreshTopBloggerMainEventListener topHandler;
//        private RefreshTopBloggerMainEvent event;
//        public SingleTask(RefreshTopBloggerMainEventListener topHandler,RefreshTopBloggerMainEvent event){
//            this.topHandler=topHandler;
//            this.event=event;
//        }
//        @Override
//        public void run() {
//            topHandler.run(event);
//
//        }
//    }
//    public void run(RefreshTopBloggerMainEvent event) {
//        List<SlimBloggerDto> bloggers = new ArrayList<>();
//        List<BloggerMain> bloggerMains = getBloggerMainsByType();
//
//        bloggerMains.forEach( b ->{
//            SlimBloggerDto slimBloggerDto = new SlimBloggerDto();
//            slimBloggerDto.setUid(b.getOUid());
//            slimBloggerDto.setRegion(b.getORegion());
//            slimBloggerDto.setId(b.getId());
//            bloggers.add(slimBloggerDto);
//        });
//
//        long start = System.currentTimeMillis();
////        XxlJobLogger.log("RefreshTopBloggerMainEvent START type:{},dataSize:{}",b.getName(),bloggers.size());
//
//        if (bloggers.size() > 0){
//            var saveBloggerEvent = new UpdateBloggerMainEventListener.UpdateBloggerMainEvent(event, bloggers,true);
//            eventBusTemplate.post(saveBloggerEvent);
//        }
////        XxlJobLogger.log("RefreshTopBloggerMainEvent END type:{},dataSize:{},user time:{}s",b.getName(),bloggers.size(),(System.currentTimeMillis()-start)/1000);
//        log.info("RefreshTopBloggerMainEvent END dataSize:{},user time:{}s",bloggers.size(),(System.currentTimeMillis()-start)/1000);
//    }
//    /*public void run(BloggerType b,RefreshTopBloggerMainEvent event) {
//        List<BloggerMain> bloggerMains = bloggerClient.getBloggerRanking(b.getName().equals("ALL")?"":b.getName(),1000);
//        List<SlimBloggerDto> bloggers = new ArrayList<>();
//
//        bloggerMains.forEach( bloggerMain -> {
//            SlimBloggerDto slimBloggerDto = new SlimBloggerDto();
//            slimBloggerDto.setUid(bloggerMain.getOUid());
//            slimBloggerDto.setRegion(bloggerMain.getORegion());
//            slimBloggerDto.setId(bloggerMain.getId());
//            bloggers.add(slimBloggerDto);
//        });
//
//        long start = System.currentTimeMillis();
//        XxlJobLogger.log("RefreshTopBloggerMainEvent START type:{},dataSize:{}",b.getName(),bloggers.size());
//
//        if (bloggers.size() > 0){
//            var saveBloggerEvent = new UpdateBloggerMainEventListener.UpdateBloggerMainEvent(event, bloggers,true);
//            eventBusTemplate.post(saveBloggerEvent);
//
//            // 更新记录的博主信息
//            bloggerMains = bloggerClient.getBloggerRanking(b.getName().equals("ALL")?"":b.getName(),1000);
//            var saveBloggerRecordEvent = new SaveBloggerRecordEventListener.SaveBloggerRecordEvent(event,bloggerMains);
//            eventBusTemplate.post(saveBloggerRecordEvent);
//        }
//        XxlJobLogger.log("RefreshTopBloggerMainEvent END type:{},dataSize:{},user time:{}s",b.getName(),bloggers.size(),(System.currentTimeMillis()-start)/1000);
//    }*/
//
//
//    private List<BloggerMain> getBloggerMainsByType(){
//        List<BloggerMain> bloggerMains = new ArrayList<>();
//        this.bloggerTypes.forEach(b->{
//            List<BloggerMain> bloggerMainsTmp = bloggerClient.getBloggerRanking(b.getName().equals("ALL")?"":b.getName(),1000);
//            bloggerMains.addAll(bloggerMainsTmp);
//        });
//        return bloggerMains;
//    }
//
//    public static class RefreshTopBloggerMainEvent extends Event {
//
//        public RefreshTopBloggerMainEvent() {
//            super("RefreshTopBloggerMain");
//        }
//    }
//}
